//
//  NSString+boundRect.m
//  KSBridgeDesign
//
//  Created by huzhaoke on 2022/1/9.
//

#import "NSString+boundRect.h"

@implementation NSString (boundRect)

- (CGSize)ks_sizeWithFont:(UIFont *)font
{
    NSParameterAssert(font);
    CGSize size = [self sizeWithAttributes:@{NSFontAttributeName:font}];
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
}

- (CGSize)ks_sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    return [self ks_sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
}

- (CGSize)ks_sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
{
    CGRect expectedFrame = [self boundingRectWithSize:size
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName: font}
                                              context:nil];
    expectedFrame.size.width = MIN(ceilf(expectedFrame.size.width), size.width);
    expectedFrame.size.height = MIN(ceilf(expectedFrame.size.height), size.height);
    return expectedFrame.size;
}

- (CGSize)ks_sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode
{
    return [self ks_sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
}


@end
